package com.zf.yichat.service.impl;

import com.github.pagehelper.PageHelper;
import com.zf.yichat.dto.NoticeCountDto;
import com.zf.yichat.dto.NoticeDto;
import com.zf.yichat.mapper.NoticeMapper;
import com.zf.yichat.mapper.NoticeViewMapper;
import com.zf.yichat.model.Notice;
import com.zf.yichat.model.NoticeView;
import com.zf.yichat.service.NoticeService;
import com.zf.yichat.utils.common.DateUtils;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.response.FsPage;
import com.zf.yichat.utils.response.FsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 一句话描述功能
 *
 * @author fengsong
 * @date create in 17:58 2020/3/19 2020
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private NoticeViewMapper noticeViewMapper;

    @Override
    public FsResponse selectList(FsPage page, Long userId) {
        Example example = new Example(Notice.class);
         example.createCriteria().andEqualTo("status", 0) ;
        example.setOrderByClause(" id desc ");
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        List<Notice> notices = noticeMapper.selectByExample(example);
//
//        //将所有未读变已读
//        List<Long> unViewList = noticeViewMapper.selectUnviewList(userId);
//        for (Long unViewNoticeId : unViewList) {
//            view(unViewNoticeId, userId);
//        }

        return DtoChangeUtils.getPageList(notices, v -> {
            NoticeDto dto = new NoticeDto();
            dto.setNoticeId(v.getId());
            dto.setContent(v.getContent());
            dto.setTimeDesc(DateUtils.formatDate(v.getCtime(), "yyyy-MM-dd HH:mm:ss"));
            dto.setTitle(v.getTitle());
            dto.setViewStatus(Objects.nonNull(selectViewByUserId(v.getId(), userId)) ? 1 : 0);

            selectViewByUserId(v.getId(), userId);
            return dto;
        });
    }
    @Override
    public FsResponse selectListLast( Long userId) {
        Example example = new Example(Notice.class);

        example.createCriteria().andEqualTo("status", 0);
        example.setOrderByClause(" id desc limit 1");
         List<Notice> notices = noticeMapper.selectByExample(example);


        return DtoChangeUtils.getPageList(notices, v -> {
            NoticeDto dto = new NoticeDto();
            dto.setNoticeId(v.getId());
            dto.setContent(v.getContent());
            dto.setTimeDesc(DateUtils.formatDate(v.getCtime(), "yyyy-MM-dd HH:mm:ss"));
            dto.setTitle(v.getTitle());
            dto.setViewStatus(Objects.nonNull(selectViewByUserId(v.getId(), userId)) ? 1 : 0);

            selectViewByUserId(v.getId(), userId);
            return dto;
        });
    }



    @Override
    public void view(Long noticeId, Long userId) {

        NoticeView view = new NoticeView();
        view.setNoticeId(noticeId);
        view.setUserId(userId);
        try {
            noticeViewMapper.insertSelective(view);
        } catch (Exception e) {

        }
    }

    @Override
    public NoticeDto unviewCount(Long userId) {

        NoticeCountDto dto = new NoticeCountDto();
        dto.setCount(noticeViewMapper.countUnview(userId));
        //查询最新一个
        Example example = new Example(Notice.class);
        Long[] l = {0L, userId};
        example.createCriteria().andEqualTo("status", 0).andIn("userId", Arrays.asList(l));
        example.setOrderByClause(" id desc limit 1 ");

        Notice v = noticeMapper.selectOneByExample(example);
        dto.setNoticeId(v.getId());
        dto.setContent(v.getContent());
        dto.setTimeDesc(DateUtils.formatDate(v.getCtime(), "yyyy-MM-dd HH:mm:ss"));
        dto.setTitle(v.getTitle());
        dto.setViewStatus(Objects.nonNull(selectViewByUserId(v.getId(), userId)) ? 1 : 0);

        return dto;
    }

    @Override
    public NoticeView selectViewByUserId(Long noticeId, Long userId) {
        Example example = new Example(NoticeView.class);
        example.createCriteria().andEqualTo("noticeId", noticeId).andEqualTo("userId", userId);
        return noticeViewMapper.selectOneByExample(example);
    }
}
